package DAOJPA;


import gestionnaire.NoterFilm;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.RollbackException;

import DAO.DAO;
import DAO.DAONoterFilm;

public class DAONoterFilmJPA extends DAO implements DAONoterFilm{

	static private DAONoterFilmJPA instance = null;
	
	private DAONoterFilmJPA() {
	}
		static public DAONoterFilm getInstance() {
		 	if (instance == null)
				instance = new DAONoterFilmJPA();
			 	
		 	return instance;
	}
	
	@Override
	public List<NoterFilm> getNoterFilm(int code , int code2) {
		// TODO Auto-generated method stub
		//NoterFilm noterFilm = DAO.getManager().find(NoterFilm.class, code , code2);
		return DAO.getManager().createQuery("SELECT a FROM NoterFilm a WHERE a.id.code_film = :code AND a.id.code_per = :code2 ", NoterFilm.class).setParameter("code", code).setParameter("code2", code2).getResultList();
		

	}

	@Override
	public List<NoterFilm> loadAll() {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM NoterFilm a",NoterFilm.class).getResultList();

	}

	@Override
	public int getNombreNoterFilm() {
		// TODO Auto-generated method stub
		return this.loadAll().size();
	}

	@Override
	public void save(NoterFilm a) {
		// TODO Auto-generated method stub			
				
		DAO.getManager().persist(a);
		DAO.commit();
	}

	@Override
	public void delete(NoterFilm a) {
		// TODO Auto-generated method stub
		//if(a.getFilm().getCode_film() == -1 && a.getUtilisateur().getCode_per() != -1) throw new IllegalArgumentException();
		getManager().remove(a);
		DAO.commit();
		//a.setCode_film(-1);
	}

}
